package com.xiaohe66.web.infrastructure.mybatis.task.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xiaohe66.common.value.Paging;
import com.xiaohe66.web.domain.account.value.AccountId;
import com.xiaohe66.web.domain.common.value.TagId;
import com.xiaohe66.web.domain.task.agg.Task;
import com.xiaohe66.web.domain.task.repository.TaskRepository;
import com.xiaohe66.web.domain.task.value.TaskId;
import com.xiaohe66.web.domain.task.value.TaskPoolId;
import com.xiaohe66.web.infrastructure.mybatis.common.model.TaskTagDo;
import com.xiaohe66.web.infrastructure.mybatis.common.repository.TaskTagRepositoryImpl;
import com.xiaohe66.web.infrastructure.mybatis.task.convert.TaskDoConverter;
import com.xiaohe66.web.infrastructure.mybatis.task.mapper.TaskMapper;
import com.xiaohe66.web.infrastructure.mybatis.task.model.TaskDo;
import com.xiaohe66.web.integration.AbstractMybatisService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * @author xiaohe
 * @since 2021.11.17 12:09
 */
@Repository
@RequiredArgsConstructor
@Slf4j
public class TaskRepositoryImpl
        extends AbstractMybatisService<TaskDoConverter, TaskMapper, TaskDo, Task, TaskId>
        implements TaskRepository {

    private final TaskTagRepositoryImpl taskTagRepository;

    @Override
    protected void insertImpl(Task agg) {
        super.insertImpl(agg);

        List<TaskTagDo> list = dataConverter.buildTaskTagOnlyValid(agg.getId(), agg.getTagIds());
        if (!list.isEmpty()) {
            taskTagRepository.saveBatch(list);
        }
    }

    @Override
    protected void updateImpl(Task agg, Task snapshot) {
        super.updateImpl(agg, snapshot);

        if (!agg.isSameTags(snapshot)) {

            if (log.isDebugEnabled()) {
                log.debug("更新任务标签, taskId : {}, newTags : {}, oldTags : {}",
                        agg.getId(),
                        agg.getTagIds(),
                        snapshot.getTagIds());
            }

            taskTagRepository.removeByTaskId(agg.getId());

            List<TaskTagDo> list = dataConverter.buildTaskTagOnlyValid(agg.getId(), agg.getTagIds());
            if (!list.isEmpty()) {
                taskTagRepository.saveBatch(list);
            }
        }
    }

    @Override
    protected Task getByIdImpl(TaskId id) {
        Task task = super.getByIdImpl(id);
        fillTagIds(task);
        return task;
    }

    @Override
    public List<Task> listByPoolId(AccountId createId, TaskPoolId poolId, Paging paging) {

        LambdaQueryWrapper<TaskDo> queryWrapper = new LambdaQueryWrapper<TaskDo>()
                .eq(TaskDo::getPoolId, poolId.getValue())
                .eq(TaskDo::getCreateId, createId.getValue())
                // .orderByAsc(TaskDo::getSort)
                .orderByDesc(TaskDo::getChangeTime)
                .last(paging.toLimit());

        List<TaskDo> list = list(queryWrapper);

        List<Task> taskList = dataConverter.toAgg(list);

        for (Task task : taskList) {
            fillTagIds(task);
        }

        return taskList;
    }

    protected void fillTagIds(Task task) {
        if(task != null){
            Set<TagId> tagIds = taskTagRepository.listTagIdByTaskId(task.getId());
            task.addTagIdAll(tagIds);
        }
    }
}
